﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR

public static class GizmosExtensions {

    public static void DrawGimos(this BoxCollider2D[] boxs, Color color) {
        Color _color = Gizmos.color;
        boxs.DrawGimos();
        Gizmos.color = _color;
    }

    public static void DrawGimos(this BoxCollider2D[] boxs) {
        foreach(var box in boxs) {
            box.DrawGimos();
        }
    }

    public static void DrawGimos(this BoxCollider2D box) {
        var size = box.size;
        var rx = size.x * 0.5f;
        var ry = size.y * 0.5f;
        var scale = box.transform.lossyScale;
        var rotation = box.transform.rotation;
        var offset = box.transform.position;
        Vector3[] v = new Vector3[4];
        v[0] = rotation * new Vector3(-rx * scale.x + box.offset.x, -ry * scale.y + box.offset.y, 0) + offset;
        v[1] = rotation * new Vector3(-rx * scale.x + box.offset.x, ry * scale.y + box.offset.y, 0) + offset;
        v[2] = rotation * new Vector3(rx * scale.x + box.offset.x, ry * scale.y + box.offset.y, 0) + offset;
        v[3] = rotation * new Vector3(rx * scale.x + box.offset.x, -ry * scale.y + box.offset.y, 0) + offset;
        Gizmos.DrawLine(v[0], v[1]);
        Gizmos.DrawLine(v[1], v[2]);
        Gizmos.DrawLine(v[2], v[3]);
        Gizmos.DrawLine(v[3], v[0]);
    }
    
    public static void DrawGimos(this PolygonCollider2D[] polygons, Color color) {
        Color _color = Gizmos.color;
        polygons.DrawGimos();
        Gizmos.color = _color;
    }

    public static void DrawGimos(this PolygonCollider2D[] polygons) {
        foreach(var polygon in polygons) {
            polygon.DrawGimos();
        }
    }

    public static void DrawGimos(this PolygonCollider2D polygon) {
        var scale = polygon.transform.lossyScale;
        var rotation = polygon.transform.rotation;
        var offset = polygon.transform.position;
        for (int i = 0, len = polygon.pathCount; i < len; i++) {
            var v = polygon.GetPath(i);
            for (int j = 0, len2 = v.Length - 1; j < len2; j++) {
                var v1 = rotation * new Vector3(v[j].x * scale.x + polygon.offset.x, v[j].y * scale.y + polygon.offset.y, 0) + offset;
                var v2 = rotation * new Vector3(v[j + 1].x * scale.x + polygon.offset.x, v[j + 1].y * scale.y + polygon.offset.y, 0) + offset;
                Gizmos.DrawLine(v1, v2);
            }
            var v3 = rotation * new Vector3(v[v.Length - 1].x * scale.x + polygon.offset.x, v[v.Length - 1].y * scale.y + polygon.offset.y, 0) + offset;
            var v4 = rotation * new Vector3(v[0].x * scale.x + polygon.offset.x, v[0].y * scale.y + polygon.offset.y, 0) + offset;
            Gizmos.DrawLine(v3, v4);
        }
    }
    
    public static void DrawGimos(this CircleCollider2D[] circles, Color color) {
        Color _color = Gizmos.color;
        circles.DrawGimos();
        Gizmos.color = _color;
    }

    public static void DrawGimos(this CircleCollider2D[] circles) {
        foreach(var circle in circles) {
            circle.DrawGimos();
        }
    }

    public static void DrawGimos(this CircleCollider2D circle) {
        Gizmos.DrawWireSphere(circle.offset + (Vector2)circle.transform.position, circle.radius * circle.transform.lossyScale.x);
    }

}


#endif

